#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QFont>
#if MSC_VER >=1000
    #pragma execution_character_set("UTF-8")
#endif

#include<QPixmap>
#include<QToolButton>
#include<QLineEdit>
#include"songlist.h"

#include<QTabWidget>

// 格式化时间 microseconds  -> mm:ss
static QString formatTime(qint64 microSeconds)
{
    qint64 seconds = microSeconds / 1000;
    const qint64 minutes = seconds / 60;
    seconds -= minutes * 60;
    return QStringLiteral("%1:%2")
            .arg(minutes, 2, 10, QLatin1Char('0'))
            .arg(seconds, 2, 10, QLatin1Char('0'));
}


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent,
                Qt::Window
                //                 Qt::WindowMinimizeButtonHint|

                //                Qt::WindowCloseButtonHint
                //  Qt::WindowShadeButtonHint|
                //  Qt::CustomizeWindowHint

                ),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    auto topWidget =  new QWidget(this);

    auto t_layout = new QGridLayout(topWidget);
    auto label_logo = new QLabel(this);
    label_logo->setFixedSize (48,48);
    label_logo->setStyleSheet ("border-image:url(:/img/logo.png);");

    auto tool_btn_setting = new QToolButton(this);
    tool_btn_setting->setFixedSize (24,24);
    tool_btn_setting->setStyleSheet ("border-image:url(:/img/setting.png);border-width:0;width:32;height:32;");

    connect(tool_btn_setting,&QAbstractButton::clicked,[this](bool check){
        // todo
        qDebug()<<"check = "<<check;

    });

    auto line_search = new QLineEdit(this);
    line_search->setFixedHeight (38);

    line_search->setStyleSheet ("background-color:#ffffff;border-radius:8");

    t_layout->addWidget (line_search,0,18,1,4);
    t_layout->addWidget (tool_btn_setting,0 ,30, 1,1,Qt::AlignRight);
    t_layout->addWidget(label_logo,0,0,1,1,Qt::AlignLeft);

    topWidget ->setLayout(t_layout);

    topWidget->setStyleSheet ("height:60px;width:100%;background-color:dodgerblue;");


    connect(songHandler,&EastSearchSong::shasData,[this ](QString text , int type){
        qDebug()<<"text "<<text<<" \n\n\n\n type = "<<type;
        SongList result = SongList::parseFromJSON (text);

        auto songs = result.getResult();
        if (songs.size() > 1 ){
            qDebug()<<"ing...";
            QString url = "http://music.163.com/song/media/outer/url?id="+QString::number( songs[0].getId())+".mp3";
            songHandler->gDownload(url,"/home/mint/temp/music/");
        } else {
            qDebug()<<"n...";
        }
        for (auto s:songs){
            qDebug()<<"name:"<<s.getName()<<" ,id "<<s.getId()<<", ";
        }
    });

    connect(line_search,&QLineEdit::textChanged,[this](const QString &text){
        if (text.contains ("vae")){

            songHandler->pSearch ("http://music.163.com/api/search/get?s=vae&type=1&limit=4",1);
        }
    });


    // cetenter widget

    topWidget ->setFixedHeight(60);

    label_lyric->setText("func Q ......................");
    this->setAttribute(Qt::WA_TranslucentBackground, true);
    // test play music
    // 这个路径是在 linux 下的路径
    // 具体路径为 /home/mint/temp/music/song.mp3
    //          /home     => 用户的根目录
    //          /mint     => 用户名
    //          /temp     => 普通文件夹的名称
    //          /song.mp3 => 播放音频的文件名
    playlist->addMedia(QUrl::fromLocalFile("/home/mint/temp/music/song.mp3"));
    playlist->setCurrentIndex(0);
    player->setVolume(50);
    player->setPlaylist(playlist);
    //  player->play();

    // 当前的 播放的总的时间发生改变
    connect(player,&QMediaPlayer::durationChanged,[this](qint64 duration){
        seek_bar->setRange(0, duration);
        qDebug()<<duration;

    });

    // 当前的 播放的进度条发生改变 。。    更新可视化播放进度
    connect(player, &QMediaPlayer::positionChanged,[this](qint64 position){
        seek_bar->setValue(position);
    });



    // 固定高度
    statusBar()->setFixedHeight(60);
    statusBar()->setStyleSheet("height:60px;width:100%;background-color:dodgerblue;");

    setWindowTitle("Q.音乐播放器");



    // 初始化状态栏
    init_status_bar();

    // playlist_view ->setStyleSheet("background-color:#333");
    playlist_view ->setParent(nullptr);
    auto center_widget = new QWidget(this);
    //  center_widget->setStyleSheet("background-image:url(:/img/music_bg.jpg)");

    // center_widget ->setStyleSheet("background-color:#333");

    auto option_widget = new QWidget(center_widget);
    option_widget ->setStyleSheet("background-color:#333");
    QHBoxLayout *option_layout = new QHBoxLayout(option_widget);
    QPushButton *btn_tool_lyric = new QPushButton(option_widget);
    QPushButton *btn_tool_sng = new QPushButton(option_widget);

    option_layout ->addWidget(btn_tool_lyric);
    option_layout ->addWidget(btn_tool_sng);
    btn_tool_sng->setText("单曲");
    btn_tool_lyric->setText("歌词");
    topWidget->setParent(center_widget);
    playlist_view ->setParent(center_widget);
    label_lyric->setParent(center_widget);
    label_lyric->setAlignment(Qt::AlignCenter);
    connect(this,&MainWindow::viewPortSizeChanged,[this,topWidget,center_widget,option_widget](QSize sz){
        topWidget->setGeometry(0,0,sz.width(),60);
        static int fix =20;
        int w = sz.width();
        auto f_1_2_20 = w / fix;

        this -> playlist_view->setGeometry(8,60+4,6* f_1_2_20,sz.height() - 128);
        option_widget->setGeometry(6* f_1_2_20+8,60,w - 5* f_1_2_20,40);
        this ->  label_lyric->setGeometry(6* f_1_2_20+10,60 +40,w - 5* f_1_2_20, sz.height()-80);
        qDebug()<<"list"<< this->playlist_view->geometry()<<this->playlist_view ->parent()<<this->label_lyric->geometry();
        qDebug()<<sz;
        this ->label_lyric ->setText("func Q");
    });


    setCentralWidget(center_widget);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::init_status_bar()
{

    widget->setLayout(base_status_layout);
    btn_pre->setParent(widget);
    btn_pause->setParent(widget);
    btn_next->setParent(widget);

    btn_pre->setStyleSheet("border-image:url(:/img/prev.png)");
    btn_pause->setStyleSheet("border-image:url(:/img/play.png)");
    btn_next->setStyleSheet("border-image:url(:/img/next.png)");


    hboxLayout->addWidget(btn_pre);
    hboxLayout->addWidget(btn_pause);
    hboxLayout->addWidget(btn_next);

    btn_pre->setFixedSize (32,32);
    btn_pause ->setFixedSize(32,32);
    btn_next ->setFixedSize (32,32);

    base_status_layout->addItem(hboxLayout,0,0,1,3);


    connect(btn_pre,&QPushButton::clicked,[](){

    });

    connect(btn_pause,&QPushButton::clicked,[this](){

        static bool isPause = false;

        if (isPause){
            player->play();
            btn_pause->setStyleSheet("border-image:url(:/img/play.png)");

        } else {
            player ->pause();
            btn_pause->setStyleSheet("border-image:url(:/img/pause.png)");

        }

        isPause = !isPause;
    });

    base_status_layout->setHorizontalSpacing(10);
    base_status_layout ->setVerticalSpacing(8);

    // 设置可视化的播放进度
    visual_bar->addWidget(seek_bar,2,0,1,18);
    QFont f;
    f.setPointSize(10);

    label_info->setFont(f);
    label_info->setText("Q.播放器");
    label_progress->setText("00:00/00:00");

    visual_bar->addWidget(label_info,0,0,1,4);
    visual_bar->addWidget(label_progress, 0,14,1,4);
    seek_bar->setParent(widget);
    base_status_layout->addLayout(visual_bar,0,5,1,18);


    seek_bar->setStyleSheet("background-color:#ffffff");


    // 松开 滑块 ， 设置播放的进度
    connect (seek_bar,&QAbstractSlider::sliderReleased,[this](){
        qDebug()<<"value = "<<seek_bar->value();
        player->setPosition(seek_bar->value() );
    });

    label_v ->setFixedSize (24,24);
    label_v->setStyleSheet("border-image:url(:/img/volume.png)");
    label_v->setToolTip("vol....");

    base_status_layout->addWidget(label_v,0,24,1,2);

    seek_vol->setParent(label_v);
    seek_vol->setVisible(false);

    statusBar()->addWidget(widget);



    //



}

//
void MainWindow::resizeEvent(QResizeEvent *event)
{
    auto s =    event->size ();

    //  发送窗口大小发生变化的信息
    emit viewPortSizeChanged(s);
    QWidget::resizeEvent(event);
}










